package com.szyjjk.imageCrawler;


import com.szyjjk.entity.SysImageCrawlerFiles;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;


/**
 * @Author yangyue
 * @Description //TODO httpDown下载
 * @Date 16:08 2019/4/23
 * @Param
 * @return
 **/
public class MODDownloadThread extends Thread {



    // 保存路径
    private String savePath;

    // wget存放位置
    private String wgetPath;


    private SysImageCrawlerFiles sysImageCrawlerFiles;

    public MODDownloadThread(  String savePath, String wgetPath, SysImageCrawlerFiles  sysImageCrawlerFiles) {
        this.savePath = savePath;
        this.wgetPath = wgetPath;
        this.sysImageCrawlerFiles = sysImageCrawlerFiles;
    }

    /**
     * wget 下载文件
     **/
    public void run() {

        String dwonUrl = this.sysImageCrawlerFiles.getFileDownloadUrl().replaceAll("https://nrt3.modaps.eosdis.nasa.gov/archive/allData/", "https://nrt3.modaps.eosdis.nasa.gov/api/v2/content/archives/allData/");
        String fileNmae = CrawlerTool.getPathHtmlName(dwonUrl);
        CrawlerTool.systemout("需要下载的为文件为：" + fileNmae, sysImageCrawlerFiles.getFileType());
//        String DownLoadresult = CrawlerTool.wgetDown(dwonUrl, WholeSysImageCrawlerInfo.getSavePath(), fileNmae, WEGETPATH);
        long begin = System.currentTimeMillis();
        long fileLength=0;
        try {
            //截取出Temp
            String filenameHead = fileNmae.replaceAll(".hdf", "") + "_Temp.hdf";

            String downulr = "cmd /c start  " + wgetPath + "  -e robots=off -m -nd -R .html,.tmp -nH --cut-dirs=4 \"DOWNURL\" --header \"Authorization: Bearer 6B9F7954-90F1-11E9-9988-815845326583\" -O " + this.savePath + "/" + filenameHead;

            downulr = downulr.replaceAll("DOWNURL", dwonUrl);
            Process process = Runtime.getRuntime().exec(downulr);
            if (process != null) {
                process.getOutputStream().close();
            }

            InputStream in = process.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String tmp = null;
            while ((tmp = br.readLine()) != null) {
                // do nothing...啥也不干
            }
            Thread.sleep(500);
            //下载完成
//            System.out.println(savePath + filenameHead+"   真正的名字:   "+fileNmae);
            File file = new File(savePath + filenameHead);
            if (file.exists()) {
                File newfile = new File(file.getParent() + File.separator + fileNmae);//创建新名字的抽象文件
                if (file.renameTo(newfile)) {
                    CrawlerTool.systemout("重命名成功：" + fileNmae, sysImageCrawlerFiles.getFileType());
                    fileLength=newfile.length();
                } else {
                    CrawlerTool.systemout("重命名失败！新文件名已存在,文件删除，文件名" + fileNmae, sysImageCrawlerFiles.getFileType());
                    file.delete();
                }
            } else {
                CrawlerTool.systemout("重命名文件不存在" + fileNmae, sysImageCrawlerFiles.getFileType());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        long mills = System.currentTimeMillis() - begin;
        CrawlerTool.systemout(fileNmae+"文件，下载耗时" + CrawlerTool.formatDuring(mills) + "s", sysImageCrawlerFiles.getFileType());
        if (mills < 1000) {
            CrawlerTool.systemout(fileNmae + "下载存在问题，耗时" + CrawlerTool.formatDuring(mills), sysImageCrawlerFiles.getFileType());
        } else {
            //存库
            if(!sysImageCrawlerFiles.getFileSize().equals(fileLength+"")){
                
            }
            sysImageCrawlerFiles.setFileSize(fileLength+"");
            CrawlerFlow.saveDataMod(sysImageCrawlerFiles);
        }
    }

}